package com.tech.koufu.ui.activity.setting;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.Request.Method;
import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.tech.koufu.MyApplication;
import com.tech.koufu.R;
import com.tech.koufu.action.RequestQueueSingleton;
import com.tech.koufu.tools.CookieStringRequest;
import com.tech.koufu.tools.Statics;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class AdviceReportActivity extends Activity{
	private static final String TAG = AdviceReportActivity.class.getName();
	private ImageView img_callback;
	private TextView tv_title;
	private EditText ed_setting_advice;
	private Button btn_sendreport;
	String ed_advice;
	private CookieStringRequest stringRequest;
	private Context context;
	private MyApplication myApp;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_advicereport);
		initView();
	}

	private void initView(){
		myApp = (MyApplication)getApplication();
		tv_title = (TextView)findViewById(R.id.tv_title);
		tv_title.setVisibility(View.VISIBLE);
		tv_title.setText("意见反馈");
		img_callback = (ImageView)findViewById(R.id.img_callback);
		img_callback.setVisibility(View.VISIBLE);
		img_callback.setOnClickListener(new myOnClickListener());
		ed_setting_advice = (EditText)findViewById(R.id.ed_setting_advice);
		btn_sendreport = (Button)findViewById(R.id.btn_sendreport);
		btn_sendreport.setOnClickListener(new myOnClickListener());
	}
	
	private class myOnClickListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.img_callback:
				AdviceReportActivity.this.finish();
				break;
			case R.id.btn_sendreport:
				ed_advice = ed_setting_advice.getText().toString();
				if(ed_advice.length() <= 10){
					Toast.makeText(getApplicationContext(), "意见不可少于10个字符哟！", Toast.LENGTH_SHORT).show();
				}else{
					Log.w("id==>context==>name", myApp.digitalid + "||" + ed_advice + "||" + myApp.userName);
					SendAdviceReport();
				}
				break;
			default:
				break;
			}
		}
		
	}
	
	private void SendAdviceReport(){
		String url = Statics.URL_PHP + Statics.IF_adviceReport;
		
		stringRequest = new CookieStringRequest(Method.POST, url, new Listener<String>() {

			@Override
			public void onResponse(String response) {
				if(!TextUtils.isEmpty(response)){
					try {
						JSONObject obj = new JSONObject(response);
						Log.w("AdviceReport====>", response);
						if(obj.getInt("status") == 0){
							Toast.makeText(getApplicationContext(), obj.getString("data"), Toast.LENGTH_SHORT).show();						
						}else{
							Toast.makeText(getApplicationContext(), obj.getString("data"), Toast.LENGTH_SHORT).show();
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}, new Response.ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) {
				// TODO Auto-generated method stub
				
			}			
		}){
			@Override
			protected Map<String, String> getParams() throws AuthFailureError {
				Map<String, String> params = new HashMap<String, String>();
				params.put("userID", myApp.digitalid);
				params.put("content", ed_advice);
				params.put("userName", myApp.userName);
				return params;
			}
		};
		stringRequest.setTag(TAG);
		RequestQueueSingleton.getInstance(context).addToRequestQueue(stringRequest);
	}
}
